home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 108_01 / conout.c < prev    next >
Text File  |  1985-11-13  |  4KB  |  131 lines

  1.  
  2. /**********************************************************
  3.  ***                            ***
  4.  ***    Copyright (c) 1981 by David M. Fogg        ***
  5.  ***                            ***
  6.  ***        2632 N.E. Fremont            ***
  7.  ***        Portland, OR 97212            ***
  8.  ***                            ***
  9.  ***        (503) 288-3502{HM} || 223-8033{WK}        ***
  10.  ***                            ***
  11.  ***    Permission is herewith granted for non-     ***
  12.  ***    commercial distribution through the BDS C    ***
  13.  ***    User's Group; any and all forms of commercial   ***
  14.  ***    redistribution are strenuously unwished-for.    ***
  15.  ***                            ***
  16.  **********************************************************/
  17.  
  18. /* ---> CONOUT <---   (Gen final Concordance from raw sorted file)
  19.  
  20.    Conceived & coded Nov 1980 by David Fogg - Portland, Oregon
  21.    Copyright (C) 1980 by David Fogg & New Day Computing Co.
  22.  
  23.    6 Nov - creation day
  24.    7 Nov - add word numbers, printer width opt
  25.  
  26.    This program takes a file named <FILNAM>.CON consisting of lines
  27.    containing: "WORD NUMBER"; the words are sorted; the numbers are
  28.    ascending for identical consecutive words.  An output file named
  29.    <FILNAM>.CCO is generated in which identical words have been re-
  30.    moved and all their line numbers combined - i.e., a concordance.
  31.  
  32.    The input file is deleted at the end.
  33.  
  34.    usage is: conout FILNAM [ODRIV]
  35.  
  36. */
  37.  
  38. #include <std.h>
  39.  
  40. #define MAXWD  50
  41. #define DEFWID 75
  42.  
  43. main (ac, av)
  44. int ac;
  45. char *av[];
  46. {
  47.    char ibuf[BUFSIZ];          /* infile buffer */
  48.    char obuf[BUFSIZ];          /* outfile buffer */
  49.    char ilin[MAXLINE];          /* input line */
  50.    char oword[MAXWD];          /* previous word */
  51.    char inam[15], onam[15];   /* in/out file names */
  52.    int spos;              /* space pos in ilin */
  53.    int lino;              /* line number from ilin */
  54.    int cpos;              /* curr pos in output line */
  55.    int fpos;              /* first pos in out lns 4 curr wd */
  56.    int ocols;              /* # of output columns */
  57.    int wdno;              /* word number */
  58.    int argn;              /* command line argument # */
  59.    char *arg;              /* pointo curr arg */
  60.  
  61. /* ---> INITIALIZATION <---
  62. */
  63.    strcpy(oword, "Potrzebie");
  64.    ocols = DEFWID;
  65.    wdno = 0;
  66.  
  67. /* ---> GET ARGUMENT(S) <---
  68. */
  69.    if (ac < 2) {
  70.       puts("usage: conout IFILE [ODRIV] [-W#]\n");
  71.       puts("IFILE: IFILE.CON is input file\n");
  72.       puts("ODRIV: (e.g., B:) where to put IFILE.CCO\n");
  73.       puts("  -W#: set printer width to '#' (default 75)");
  74.       exit ();
  75.    }
  76.  
  77.    for (argn = 2; argn < ac; ++argn) {       /* get/set options */
  78.       arg = av[argn];
  79.       if (*arg == '-')
  80.      switch (arg[1]) {
  81.         case 'W': ocols = atoi(arg+2);
  82.               if (ocols < 20)
  83.             errxit("Bad printer width value");
  84.               break;
  85.         default :
  86.            errxit("Bad option value");
  87.      }
  88.    }
  89.  
  90.  
  91.    strcpy(inam, av[1]);            /* setup infile */
  92.    strcat(inam, ".CON");
  93.    if (fopen(inam, ibuf) == ERROR)
  94.       errxit("Input file error");
  95.  
  96.    if (ac > 2 && *(av[2]+1) == ':') {  /* setup outfile */
  97.       strcpy(onam, av[2]);
  98.       if (strlen(onam) > 2) errxit("Bad output drive field");
  99.       strcat(onam, av[1] + instr(av[1],":"));
  100.    }
  101.    else
  102.       strcpy(onam, av[1]);
  103.    strcat(onam, ".CCO");
  104.    if (fcreat(onam, obuf) == ERROR)
  105.       errxit("Output file error");
  106.  
  107. /*
  108.    >>------> MAIN LOOP <------<<
  109. */
  110.    while (fgets(ilin, ibuf)) {
  111.       spos = instr(ilin, " ");
  112.       ilin[spos-1] = NULL;
  113.       lino = atoi(ilin+spos);
  114.       if (strcmp(oword, ilin)) {
  115.      fprintf(obuf, "\n%4d %s %5d", ++wdno, ilin, lino);
  116.      fpos = strlen(ilin) + 6; cpos = fpos + 5;
  117.      strcpy(oword, ilin);
  118.       }
  119.       else {
  120.      if (cpos > ocols - 3) {
  121.         fputs("\n ", obuf); cpos = 0;
  122.         while (++cpos < fpos) putc(' ', obuf);
  123.      }
  124.      fprintf(obuf, "%5d", lino);
  125.      cpos += 5;
  126.       }
  127.    }
  128.  
  129.    fclose(ibuf); oclose(obuf);
  130. }
  131.